package com.ztgf.sys.service;

import com.alibaba.fastjson.JSON;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.common.vo.UserVO;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.sys.models.constant.SysConstant;
import com.ztgf.sys.models.dto.SysDictDTO;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISessionService;
import com.ztgf.sys.models.service.ISysDictService;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.service.IUserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
@Slf4j
public class SessionServiceImpl implements ISessionService {

    @DubboReference
    private IUserLoginService userLoginService;
    @Autowired
    private ISysUserService sysUserService;
    @DubboReference
    private ISysDictService sysDictService;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Result isSessionExpired(String sessionId) {
        Result result = new Result();
        //验证session
        UserInfoDTO userInfoDTO = userLoginService.getUserBySessionId(sessionId);
        if(userInfoDTO == null){
            SysUserDTO sysUserDTO = sysUserService.getUserBySessionId(sessionId);
            if(sysUserDTO == null){
                SysDictDTO sysDictDTO = sysDictService.getByName(SysConstant.SSO_OPEN);
                if (sysDictDTO != null && sysDictDTO.getValue().equals("1")){
                    //启用sso
                    result = sysUserService.validateToken(sessionId, CommonParameter.getHeadParamsBO().getClientIp());
                }else {
                    //没启用sso
                    result = new Result(CodeEnum.FAIL_2);
                }
            } else {
                redisUtil.setSession(sessionId, sysUserDTO, CommonParameter.getHeadParamsBO().getSource().toString());
                redisUtil.setIdSessionRelation(sysUserDTO.getId().toString(), sessionId, CommonParameter.getHeadParamsBO().getSource().toString());
                result.setData(sysUserDTO);
            }
        } else {
            //设置session
            redisUtil.setSession(sessionId, userInfoDTO, CommonParameter.getHeadParamsBO().getSource().toString());
            redisUtil.setIdSessionRelation(userInfoDTO.getId().toString(), sessionId, CommonParameter.getHeadParamsBO().getSource().toString());
            result.setData(userInfoDTO);
        }

        return result;
    }

    @Override
    public Long getUserId() {
        Long createUserId = null;
        Result result = isSessionExpired (CommonParameter.getHeadParamsBO ().getSessionId ());
        System.out.println (result.success ()+"=======result ===="+CommonParameter.getHeadParamsBO ().getSessionId ()+"========== " + JSON.toJSONString (result.getData ()));
        if(result.success ()){
            if(result.getData () instanceof UserInfoDTO){
                System.out.println ("if++++++++++++++++++++++++++++++++++++++++ " );
                createUserId = ((UserInfoDTO) result.getData ()).getId ();
            } else {
                System.out.println ("else++++++++++++++++++++++++++++++++++++++++ " );
                createUserId = ((SysUserDTO)result.getData ()).getId ();
            }
        }
        return createUserId;
    }

    @Override
    public UserVO getUserVO() {
        String sessionId = CommonParameter.getHeadParamsBO ().getSessionId ();
        String source = CommonParameter.getHeadParamsBO ().getSource ().toString ();
        return getUserVO(sessionId, source);
    }

    @Override
    public UserVO getUserVO(String tokenId, String source) {
        UserVO userVO = new UserVO();

        Object object = redisUtil.getSession(tokenId, source);

        log.error("--------tokenId:"+tokenId + "----source:" + source+"----object:"+JSON.toJSONString(object));
        if(object == null){
            return null;
            //throw new ServiceException(CodeEnum.FAIL_100002);
        }

        if(object instanceof UserInfoDTO){
            userVO.setId(((UserInfoDTO) object).getId());
            userVO.setName(((UserInfoDTO) object).getName());
            userVO.setType(0);
        } if(object instanceof SysUserDTO){
            userVO.setId(((SysUserDTO) object).getId());
            userVO.setName(((SysUserDTO) object).getName());
            userVO.setType(1);
        }

        return userVO;
    }
}
